#include <stdio.h>
#include "co_log.h"
#include <stdarg.h>
#include "FreeRTOS.h"
#include "semphr.h"


static SemaphoreHandle_t printMutex;

void init_printf_mutex(void)
{

    printMutex = xSemaphoreCreateMutex();
    if (printMutex == NULL) {
      
    }
}

//线程安全打印接口
void thread_safe_printf(const char *format, ...) 
{
    va_list args;
    va_start(args, format);
    if(xPortIsInsideInterrupt()) {
        if (xSemaphoreTakeFromISR(printMutex, NULL)) {
            vprintf(format, args);
            xSemaphoreGiveFromISR(printMutex, NULL);
        }
    }else
    {
        if (xSemaphoreTake(printMutex, portMAX_DELAY)) {
            vprintf(format, args);
            xSemaphoreGive(printMutex);
        }
    }
    va_end(args);
}

